﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CarLeasing.Models;
using CarLeasing.Data;

namespace CarLeasing.Forms
{
    public partial class AllCarForm : Form
    {
        public AllCarForm()
        { }

        public AllCarForm(User user, LocalStore store)
        {
            User = user;
            Store = store;
            InitializeComponent();
        }

        private void AllCarForm_Load(object sender, EventArgs e)
        {
            carInfoListPanel.CarSelected += CarSeleted;
            ResetFilter();
        }

        private void CarSeleted(CarInfo car)
        {
            if (car == null)
            {
                button_FavoriteInOut.Text = "";
                button_FavoriteInOut.Enabled = false;
            }

            button_FavoriteInOut.Enabled = true;
            if (User.Favorites(Store).Any(x => x.Id == car.Id))
            {
                button_FavoriteInOut.Text = "Remove from My Favorites";
                FavoriteBehavior = () =>
                {
                    User.RemoveFavorite(car, Store);
                    CarSeleted(car);
                };
            }
            else
            {
                button_FavoriteInOut.Text = "Add to My Favorites";
                FavoriteBehavior = () =>
                {
                    User.AddFavorite(car, Store);
                    CarSeleted(car);
                };
            }
        }

        private void button_FavoriteInOut_Click(object sender, EventArgs e) => FavoriteBehavior();

        private void button_OpenFavorites_Click(object sender, EventArgs e)
        {
            using (var favorites = new FavoritesForm(User, Store))
                favorites.ShowDialog();
        }

        private void SetFilter()
        {
            var cars = Store.Of<CarInfo>().ToArray();
            using (var filterForm = new FilterForm(cars.Select(x => x.Origin).ToArray(), cars.Select(x => x.Type).ToArray(), cars.Select(x => x.Price).ToArray()))
            {
                if (filterForm.ShowDialog() == DialogResult.OK)
                {
                    carInfoListPanel.LoadCarInfos(Store.Of<CarInfo>().Where(filterForm.Filter).ToArray());
                    button_Filter.Text = "清除筛选";
                    FilterBehavior = ResetFilter;
                }
            }
        }

        private void ResetFilter()
        {
            carInfoListPanel.LoadCarInfos(Store.Of<CarInfo>().ToArray());
            button_Filter.Text = "筛选";
            FilterBehavior = SetFilter;
        }

        private void button_Filter_Click(object sender, EventArgs e) => FilterBehavior();

        private Action FavoriteBehavior = () => {; };
        private Action FilterBehavior = () => {; };
        private readonly User User;
        private readonly LocalStore Store;
    }
}
